#include <stdio.h>


void swap(int *a, int *b)
{
	int tmp;
	tmp = *a;
	*a = *b;
	*b = tmp;
}

int next(int* a, int n)
{
	int i, j;

	for(i = 1; i < n; i++) {
		if(a[i] < a[i - 1]) { /*find the first pos can adjust */
			break;
		}
	}

	if(i >= n) { /* adjust finished */
		return 0;
	}

	for(j = 0; j <= i - 1; j++) {
		if(a[j] > a[i]) {
			break;
		}
	}

	swap(a + i, a + j);

	for(i = i - 1, j = 0; i > j; i--, j++) { /* sort ? */
		swap(a + i, a + j);
	}

	return 1;
}


int main(void)
{
	int a[5] = {5, 4, 3, 3, 1}; /* this array was sorted */
	//int a[5] = {3, 3, 4, 1, 5}; /* this array was sorted */
	int cnt = 0, i;

	do {
		for(i = 4; i >= 0; i--) {
			printf("%d ", a[i]);
		}
		printf("\n");
		cnt++;
	} while(next(a, 5));

	printf("%d\n", cnt);
	getchar();
	return 0;
}

